package com.ssy.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.ssy.common.CodeMsg;
import com.ssy.common.PageInfo;
import com.ssy.common.Result;
import com.ssy.dao.ScoreDao;
import com.ssy.entity.Score;
import com.ssy.entity.Student;
import com.ssy.service.ClassService;
import com.ssy.service.CurriculumService;
import com.ssy.service.ScoreService;
import com.ssy.service.StudentService;
import com.ssy.vo.ScoreVo;

import java.util.HashMap;

/**
 * ScoreServiceImpl
 *
 * @author ssy
 * @date 2020/11/21
 **/
public class ScoreServiceImpl implements ScoreService {

    ScoreDao scoreDao = new ScoreDao();
    CurriculumService curriculumService = new CurriculumServiceImpl();
    StudentService studentService = new StudentServiceImpl();

    @Override
    public Result BathdeleteScore(String[] ids) {
        boolean flag = this.scoreDao.batchDeleteScore(ids);
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result deleteOneScore(String id) {
        int idNo = Integer.parseInt(id);
        boolean flag = this.scoreDao.deleteOneScore(idNo);
        return flag ? new Result(CodeMsg.DELETE_SUCCES) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result addScore(String studentNumberId, String courseNameId, String fraction) {
        int courseNameIdNo = Integer.parseInt(courseNameId);
        int fractionNo = Integer.parseInt(fraction);

        //取学生ID 班级ID
        Student student = this.studentService.selectStudentByStudentNum(studentNumberId);
        if (student == null) {
            return new Result(CodeMsg.INVALID_STUDENT_NUMBER);//无效学号
        }
        int studentId = student.getsId();
        int classId = student.getSclass();

        //取cumId
        int cumId = this.curriculumService.queryCurriculumByClassIdAndCourseId(classId, courseNameIdNo);
        if (cumId == -1) {
            return new Result(CodeMsg.ADD_SCORE_ERROR);//该学生没有这门课程
        }

        //防止成绩重复添加
        Score score = this.scoreDao.queryScoreBystudentIdAndCourseId(studentId, courseNameIdNo);
        if (score != null) {
            return new Result(CodeMsg.ADD_SCORE_EXISTS_ERROR);
        }

        boolean flag = this.scoreDao.insertScore(studentId, courseNameIdNo, cumId, fractionNo);
        return flag ? new Result(CodeMsg.ADD_SUCCES) : new Result(CodeMsg.ADD_ERROR);

    }

    @Override
    public Result updateScore(String studentNumberId, String courseNameId, String fraction, String scoreId) {
        int courseNameIdNo = Integer.parseInt(courseNameId);
        int fractionNo = Integer.parseInt(fraction);
        int scoreIdNo = Integer.parseInt(scoreId);

        //取学生ID 班级ID
        Student student = this.studentService.selectStudentByStudentNum(studentNumberId);
        if (student == null) {
            return new Result(CodeMsg.INVALID_STUDENT_NUMBER);//无效学号
        }
        int studentId = student.getsId();
        int classId = student.getSclass();

        //取cumId
        int cumId = this.curriculumService.queryCurriculumByClassIdAndCourseId(classId, courseNameIdNo);
        if (cumId == -1) {
            return new Result(CodeMsg.ADD_SCORE_ERROR);//该学生没有这门课程
        }
        boolean flag = this.scoreDao.modifyScore(studentId, courseNameIdNo, cumId, fractionNo, scoreIdNo);
        return flag ? new Result(CodeMsg.UPDATE_SUCCES) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result queryPage(HashMap<String, Object> param, String page, String limit) {
        Integer pageNo = NumberUtil.isNumber(page) ? Integer.parseInt(page) : 1;
        Integer limitNo = NumberUtil.isNumber(limit) ? Integer.parseInt(limit) : 10;
        PageInfo<ScoreVo> userPageInfo = this.scoreDao.selectPage(param, pageNo, limitNo);
        return new Result(userPageInfo);
    }
}
